//
//  LeveWorldViewController.m
//  LEVE
//
//  Created by 王若聪 on 2017/2/23.
//  Copyright © 2017年 dashuju. All rights reserved.
//

#import "LVLeveWorldViewController.h"
#import "ViewUtils.h"
#import "CMPopTipView.h"
#import "LVSayHelloViewController.h"
#import "LVThreeBtnTool.h"
#import "LVImpDynamicListVC.h"
#import "HYBaseNavigationViewController.h"
#import "LVSayHelloRelatedView.h"
#import "LVRecieveGreetingListTableViewCell.h"
#import "LVLeveWorldRoundButton.h"
#import "LVSayHelloElectricWavesView.h"
#import "RoyRippleAnimationView.h"
#import "LVCameraViewController.h"
typedef NS_ENUM(NSInteger, LVSayHelloRelatedViewType) {
    LVSayHelloRelatedViewTypeSayHello,
    LVSayHelloRelatedViewTypeNoResponse,
    LVSayHelloRelatedViewTypeNotFound,
    LVSayHelloRelatedViewTypeRecommend
};
@interface LVLeveWorldViewController ()<CMPopTipViewDelegate,UITableViewDelegate,UITableViewDataSource>
//mainView
@property (strong, nonatomic) LLSimpleCamera        *camera;
@property (strong, nonatomic) UILabel               *errorLabel;
@property (strong, nonatomic) UIButton              *listButton;
@property (strong, nonatomic) UIButton              *ghostButton;
@property (strong, nonatomic) UIButton              *switchToCameraButton;
@property (strong, nonatomic) UIButton              *selectToolsButton;
@property (strong, nonatomic) CMPopTipView          *roundRectButtonPopTipView;
//sayHelloView
@property (strong, nonatomic) UIVisualEffectView    *sayHelloBlurEffectView;
@property (strong, nonatomic) LVSayHelloRelatedView *sayHelloRelatedView;
@property (strong, nonatomic) UIButton              *cancelSayHelloViewBtn;
@property (strong, nonatomic) UITableView           *recieveGreetingTableView;
@property (strong, nonatomic) LVLeveWorldRoundButton*actionButton;

@property (assign, nonatomic) BOOL                  sayHelloViewHasShown;
@property (nonatomic, strong) LVSayHelloElectricWavesView *waveView;
@property (nonatomic, assign) LVSayHelloRelatedViewType currentSayHelloRelatedViewType;
@property (nonatomic, strong) RoyRippleAnimationView *rippleView;

//创建定时器(因为下面两个方法都使用,所以定时器拿出来设置为一个属性)
@property(nonatomic,strong)NSTimer*countDownTimer;
@end

@implementation LVLeveWorldViewController

static NSInteger  secondsCountDown = 29;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    [self setupCamera];
    [self setupUI];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    // start the camera
    [self.camera start];
}

#pragma mark - MainView Button Event
- (void)greetingButtonPressed{
    
    LVThreeBtnTool *threeBtnTool = [LVThreeBtnTool shareThreeBtn];
    [threeBtnTool showOnlyMiddle];
    [threeBtnTool showOtherViewUnderDirection:LVThreeBtnCenter distance:20];
    
//    if (threeBtnTool.isCenterBtnAnimated) {
//        ////移除threeBtnTool的中间button的动画
//        [threeBtnTool removeCenterBtnWaveAnimation];
//
//    }else{
//        ////threeBtnTool的中间button的动画
//        [threeBtnTool performCenterBtnWaveAnimation];
//    }


    if (!self.sayHelloViewHasShown) {
        //相机暂停
        [self.camera stop];
        
        self.sayHelloViewHasShown = YES;
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationOnOrOffScrollMainView" object:@[@(NO),@(1)]];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationOnOrOffScrollMiddleView" object:@[@(NO),@(1)]];
        
        
        if (self.sayHelloBlurEffectView == nil) {
            [self setupSayHelloView];
            
        }else{
            self.sayHelloBlurEffectView.hidden = NO;
        }
        
    }
}

- (void)greetingButtonLongPressed{
    //接收招呼
    LVThreeBtnTool *threeBtnTool = [LVThreeBtnTool shareThreeBtn];
    [threeBtnTool showOnlyMiddle];
//    [threeBtnTool showOtherViewUnderDirection:LVThreeBtnCenter distance:20];
    
    if (!self.sayHelloViewHasShown) {
        //相机暂停
        [self.camera stop];
        
        self.sayHelloViewHasShown = YES;
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationOnOrOffScrollMainView" object:@[@(NO),@(1)]];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationOnOrOffScrollMiddleView" object:@[@(NO),@(1)]];
        
        
        if (self.sayHelloBlurEffectView == nil) {
            [self setupRecieveGreetingViewRelatedView];
            
        }else{
            self.sayHelloBlurEffectView.hidden = NO;
        }
    }
}
- (void)selectToolsButtonPressed:(UIButton *)sender{
    
}

- (void)listButtonPressed:(UIButton *)sender{
    
    LVImpDynamicListVC *impListVc = [[LVImpDynamicListVC alloc] init];
    HYBaseNavigationViewController *nav = [[HYBaseNavigationViewController alloc] initWithRootViewController:impListVc];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)ghostButtonPressed:(UIButton *)sender{
    [self popTipViewShowAt:sender];
    if (_ghostPressedBlock) {
        _ghostPressedBlock(nil);
    }
}

- (void)switchToCameraButtonPressed:(UIButton *)sender{
    LVCameraViewController *vc = [[LVCameraViewController alloc] init];
    [[LVThreeBtnTool shareThreeBtn] hideOnlyMiddleBtn:YES];
    vc.backBlock = ^(){
        [[LVThreeBtnTool shareThreeBtn] hideOnlyMiddleBtn:NO];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - recieveGreetingViewRelated
- (void)setupRecieveGreetingViewRelatedView{
    
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    self.sayHelloBlurEffectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    self.sayHelloBlurEffectView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    [self.view addSubview:self.sayHelloBlurEffectView];
    
    LVThreeBtnTool *threeBtnTool = [LVThreeBtnTool shareThreeBtn];

    UILabel *lookingForSomeoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, threeBtnTool.centerBtn.top - 10, kScreenWidth, 20)];
    lookingForSomeoneLabel.text = @"正在搜索附近的人";
    lookingForSomeoneLabel.font = [UIFont systemFontOfSize:14];
    lookingForSomeoneLabel.textAlignment = NSTextAlignmentCenter;
    lookingForSomeoneLabel.textColor = [UIColor whiteColor];
    [self.sayHelloBlurEffectView addSubview:lookingForSomeoneLabel];

    self.rippleView = [[RoyRippleAnimationView alloc]init];
    [UIView animateWithDuration:0.5f animations:^{
        threeBtnTool.centerBtn.center = CGPointMake(self.sayHelloBlurEffectView.center.x, self.sayHelloBlurEffectView.center.y-50);
    }completion:^(BOOL finished) {
        [self.rippleView rippleForTargetView:threeBtnTool.centerBtn];
    }];

    self.cancelSayHelloViewBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.cancelSayHelloViewBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelSayHelloViewBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.cancelSayHelloViewBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.cancelSayHelloViewBtn addTarget:self action:@selector(cancelSayHelloViewBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.sayHelloBlurEffectView addSubview:self.cancelSayHelloViewBtn];
    [self.cancelSayHelloViewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@80);
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-10);
        make.height.equalTo(@20);
    }];
}

#pragma mark - sayHelloViewRelated
- (void)setupSayHelloView{
    _currentSayHelloRelatedViewType = LVSayHelloRelatedViewTypeSayHello;
    
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    self.sayHelloBlurEffectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    self.sayHelloBlurEffectView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    [self.view addSubview:self.sayHelloBlurEffectView];
    
    self.sayHelloRelatedView = [LVSayHelloRelatedView viewFromNIB];
    [self.sayHelloBlurEffectView addSubview:self.sayHelloRelatedView];
    [self.sayHelloRelatedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sayHelloBlurEffectView.mas_top).offset(285);
        make.centerX.equalTo(self.sayHelloBlurEffectView);
    }];
    
    [self setupTimer];
    
    
    self.cancelSayHelloViewBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.cancelSayHelloViewBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelSayHelloViewBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.cancelSayHelloViewBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.cancelSayHelloViewBtn addTarget:self action:@selector(cancelSayHelloViewBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.sayHelloBlurEffectView addSubview:self.cancelSayHelloViewBtn];
    [self.cancelSayHelloViewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@80);
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-10);
        make.height.equalTo(@20);
    }];
    
    self.actionButton = [[LVLeveWorldRoundButton alloc]initWithFrame:CGRectMake(0, 0, 120, 30)];
    [self.actionButton setTitle:@"跳过" forState:UIControlStateNormal];
    self.actionButton.layer.opacity = 0.5;
    [self.actionButton setTitleColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5]
                     forState:UIControlStateNormal];
    self.actionButton.center = self.view.center;
    [self.actionButton addTarget:self action:@selector(actionButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.sayHelloBlurEffectView addSubview:self.actionButton];
    
    //底部的电磁波动画
    [self removeSubViews];
    [self.sayHelloBlurEffectView addSubview:self.waveView];
    [_waveView showWaveView];

    
    //测试按钮
//    UIButton *testListButton = [[UIButton alloc]init];
//    [testListButton setTitle:@"show list" forState:UIControlStateNormal];
//    [testListButton addTarget:self action:@selector(testListButtonPressed) forControlEvents:UIControlEventTouchUpInside];
//    [self.sayHelloBlurEffectView addSubview:testListButton];
//    [testListButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.equalTo(@80);
//        make.left.equalTo(self.view);
//        make.bottom.equalTo(self.view.mas_bottom).offset(-10);
//        make.height.equalTo(@20);
//    }];
    
}

- (void)setupTimer{
    //设置定时器
    _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];
    //启动倒计时后会每秒钟调用一次方法 countDownAction
    //设置倒计时显示的时间
    NSString *str_second = [NSString stringWithFormat:@"%ld",secondsCountDown];//秒
    NSString *format_time = [NSString stringWithFormat:@"%@",str_second];
    self.sayHelloRelatedView.timeLabel.text = [NSString stringWithFormat:@"%@s",format_time];
}

- (void)removeSubViews
{
    if (_waveView.superview == self.sayHelloBlurEffectView) {
        [_waveView removeFromParentView];
    }
}
- (LVSayHelloElectricWavesView *)waveView
{
    if (!_waveView) {
        LVThreeBtnTool *threeBtnTool = [LVThreeBtnTool shareThreeBtn];

        self.waveView = [[LVSayHelloElectricWavesView alloc] initWithFrame:CGRectMake(0, threeBtnTool.centerBtn.top, kScreenWidth, threeBtnTool.centerBtn.height)];
        _waveView.backgroundColor = [UIColor clearColor];
    }
    
    return _waveView;
}

- (void)updateSayHelloRelatedViewForType:(LVSayHelloRelatedViewType)type{
    _currentSayHelloRelatedViewType = type;
    
    switch (type) {
        case LVSayHelloRelatedViewTypeNotFound:
            self.sayHelloRelatedView.titleLabel.text = @"没有响应";
            self.sayHelloRelatedView.descriptionLabel1.text = @"为你推荐附近的5位用户";
            self.sayHelloRelatedView.descriptionLabel2.text = @"或许你会在这里遇到他";
            [_countDownTimer invalidate];
            secondsCountDown = 29;
            self.sayHelloRelatedView.timeLabel.hidden = YES;
            
            [self.actionButton yellowStyle];
            [self.actionButton setTitle:@"同意" forState:UIControlStateNormal];
            
            break;
        case LVSayHelloRelatedViewTypeRecommend:
            self.sayHelloRelatedView.titleLabel.text = @"为你推荐";
            self.sayHelloRelatedView.descriptionLabel1.text = @"正在为你推荐周边的小鬼";
            self.sayHelloRelatedView.descriptionLabel2.text = @"";
            
            [self setupTimer];
            self.sayHelloRelatedView.timeLabel.hidden = NO;
            
            self.actionButton.hidden = YES;
            break;
        case LVSayHelloRelatedViewTypeNoResponse:
            self.sayHelloRelatedView.titleLabel.text = @"没有响应";
            self.sayHelloRelatedView.descriptionLabel1.text = @"没有人响应你的招呼";
            self.sayHelloRelatedView.descriptionLabel2.text = @"你可以让小鬼进行漫游，也许能发现他的信息";
            [_countDownTimer invalidate];
            secondsCountDown = 29;
            self.sayHelloRelatedView.timeLabel.hidden = YES;
            
            [self.actionButton yellowStyle];
            [self.actionButton setTitle:@"小鬼设置" forState:UIControlStateNormal];
            self.actionButton.hidden = NO;
            break;

        default:
            
            break;
    }
}
#pragma mark - sayHelloViewRelated Button Event

- (void)actionButtonPressed:(UIButton*)sender{
    if (_currentSayHelloRelatedViewType == LVSayHelloRelatedViewTypeSayHello) {
        
        [self updateSayHelloRelatedViewForType:LVSayHelloRelatedViewTypeNotFound];

    }else if (_currentSayHelloRelatedViewType == LVSayHelloRelatedViewTypeNotFound){
        
        [self updateSayHelloRelatedViewForType:LVSayHelloRelatedViewTypeRecommend];

        
    }else if (_currentSayHelloRelatedViewType == LVSayHelloRelatedViewTypeRecommend){
    
    }else{
        //LVSayHelloRelatedViewTypeNoResponse
        //跳转到管理小鬼页面
        if (_ghostPressedBlock) {
            _ghostPressedBlock(nil);
        }
    }
    
}

- (void)cancelSayHelloViewBtnPressed:(UIButton *)sender{
    //移除雷达发射动画
    if (self.rippleView) {
        [self.rippleView removeFromParentView];
    };
    //移除电磁波动画
    [self removeSubViews];

    //倒计时关闭
    [_countDownTimer invalidate];
    secondsCountDown = 29;
    
    //相机恢复
    [self.camera start];
    self.sayHelloViewHasShown = NO;
    
    [UIView animateWithDuration:0.5f animations:^{
        
    }completion:^(BOOL finished) {
        if (self.sayHelloBlurEffectView) {
            self.sayHelloBlurEffectView.hidden = YES;
            self.sayHelloBlurEffectView = nil;
            
            self.recieveGreetingTableView = nil;
            
        }
        
        
        LVThreeBtnTool *threeBtnTool = [LVThreeBtnTool shareThreeBtn];
        [threeBtnTool showInTheMiddle];
        [threeBtnTool showOtherViewUnderDirection:LVThreeBtnCenter distance:30];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationOnOrOffScrollMainView" object:@[@(YES),@(1)]];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationOnOrOffScrollMiddleView" object:@[@(YES),@(1)]];
    }];
}

- (void)testListButtonPressed{
    self.sayHelloRelatedView.hidden = YES;
    [self showRecieveGreetingTableView];
}

- (void)showRecieveGreetingTableView{
    LVThreeBtnTool *threeBtnTool = [LVThreeBtnTool shareThreeBtn];
    CGRect tableViewFrame = CGRectMake(20, 0, kScreenWidth - 40, threeBtnTool.centerBtn.top - 50 - 19);
    
    if (!self.recieveGreetingTableView) {
        self.recieveGreetingTableView = [[UITableView alloc] initWithFrame:tableViewFrame style:UITableViewStyleGrouped];
        [self.sayHelloBlurEffectView addSubview:self.recieveGreetingTableView];
        self.recieveGreetingTableView.delegate = self;
        self.recieveGreetingTableView.dataSource = self;
        self.recieveGreetingTableView.backgroundColor = [UIColor clearColor];
    }else{
    
        self.recieveGreetingTableView.hidden = NO;
    }
}

#pragma mark - UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *LVRecieveGreetingListTableViewCellIdentifier = @"LVRecieveGreetingListTableViewCellIdentifier";
    LVRecieveGreetingListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LVRecieveGreetingListTableViewCellIdentifier];
    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"LVRecieveGreetingListTableViewCell" bundle:nil] forCellReuseIdentifier:LVRecieveGreetingListTableViewCellIdentifier];
        cell = [tableView dequeueReusableCellWithIdentifier:LVRecieveGreetingListTableViewCellIdentifier];
    }
//    PYSearchResultModel *model = _searchSuggestionsM[indexPath.row];
//    [cell loadCellWithModel:model];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 76;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(10, 0, tableView.frame.size.width, 30)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, tableView.frame.size.width, 18)];
    [label setFont:[UIFont boldSystemFontOfSize:11]];
    [label setTextColor:[UIColor whiteColor]];
    NSString *string =@"February 20th 8:30";
    [label setText:string];
    label.backgroundColor = [UIColor clearColor];
    headerView.backgroundColor = [UIColor clearColor];
    [headerView addSubview:label];
    return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

#pragma mark - 自定义气泡弹出提示框
- (void)popTipViewShowAt:(UIView *)targetView{
    // Toggle popTipView when a standard UIButton is pressed
    if (nil == self.roundRectButtonPopTipView) {
        UIView *tipView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 240, 44)];
        tipView.backgroundColor = [UIColor whiteColor];
        
        UILabel *welcomeLabel = [[UILabel alloc]init];
        [tipView addSubview:welcomeLabel];
        welcomeLabel.text = @"Welcome to the Leve AR!";
        welcomeLabel.font = [UIFont systemFontOfSize:13];
        //        welcomeLabel.backgroundColor = [UIColor yellowColor];
        [welcomeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(tipView.mas_left).offset(22);
            make.top.equalTo(tipView.mas_top).offset(11);
            make.height.equalTo(@20);
            make.right.equalTo(tipView.mas_right).offset(-44);
        }];
        
        UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [addBtn setImage:[UIImage imageNamed:@"greet_add"] forState:UIControlStateNormal];
        [addBtn addTarget:self action:@selector(ghostButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [tipView addSubview:addBtn];
        [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(tipView.mas_right).offset(-6);
            make.top.equalTo(tipView.mas_top).offset(6);
            make.width.and.height.equalTo(@32);
        }];
        
        self.roundRectButtonPopTipView = [[CMPopTipView alloc] initWithCustomView:tipView];
        self.roundRectButtonPopTipView.shouldEnforceCustomViewPadding = NO;
        self.roundRectButtonPopTipView.cornerRadius = tipView.height * 0.5;
        self.roundRectButtonPopTipView.topMargin = -2;
        self.roundRectButtonPopTipView.pointerSize = 7;
        //        self.roundRectButtonPopTipView = [[CMPopTipView alloc] initWithMessage:@"\n Welcome to the Leve AR! \n"];
        self.roundRectButtonPopTipView.delegate = self;
        self.roundRectButtonPopTipView.backgroundColor = [UIColor whiteColor];
        self.roundRectButtonPopTipView.textColor = [UIColor darkTextColor];
        
        [self.roundRectButtonPopTipView presentPointingAtView:targetView inView:self.view animated:YES];
    }
    else {
        // Dismiss
        [self.roundRectButtonPopTipView dismissAnimated:YES];
        self.roundRectButtonPopTipView = nil;
    }
}

#pragma mark CMPopTipViewDelegate methods
- (void)popTipViewWasDismissedByUser:(CMPopTipView *)popTipView {
    // User can tap CMPopTipView to dismiss it
    self.roundRectButtonPopTipView = nil;
}

#pragma private methods
//实现倒计时动作
-(void)countDownAction{
    //倒计时-1
    secondsCountDown--;
    NSString *str_second = [NSString stringWithFormat:@"%ld",secondsCountDown];//秒
    NSString *format_time = [NSString stringWithFormat:@"%@",str_second];
    self.sayHelloRelatedView.timeLabel.text = [NSString stringWithFormat:@"%@s",format_time];
    //当倒计时到0时做需要的操作，比如验证码过期不能提交
    if(secondsCountDown==0){
        [_countDownTimer invalidate];
        if (self.currentSayHelloRelatedViewType == LVSayHelloRelatedViewTypeSayHello) {
            [self updateSayHelloRelatedViewForType:LVSayHelloRelatedViewTypeNotFound];
        }else if(self.currentSayHelloRelatedViewType == LVSayHelloRelatedViewTypeRecommend){
            [self updateSayHelloRelatedViewForType:LVSayHelloRelatedViewTypeNoResponse];
        }
    }
}

#pragma mark - initialization Methods
- (void)setupCamera{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    // ----- initialize camera -------- //
    
    // create camera vc
    self.camera = [[LLSimpleCamera alloc] initWithQuality:AVCaptureSessionPresetHigh
                                                 position:LLCameraPositionRear
                                             videoEnabled:YES];
    
    // attach to a view controller
    [self.camera attachToViewController:self withFrame:CGRectMake(0, 0, screenRect.size.width, screenRect.size.height)];
    
    // read: http://stackoverflow.com/questions/5427656/ios-uiimagepickercontroller-result-image-orientation-after-upload
    // you probably will want to set this to YES, if you are going view the image outside iOS.
    self.camera.fixOrientationAfterCapture = NO;
    
    // take the required actions on a device change
    __weak typeof(self) weakSelf = self;
    
    [self.camera setOnError:^(LLSimpleCamera *camera, NSError *error) {
        NSLog(@"Camera error: %@", error);
        
        if([error.domain isEqualToString:LLSimpleCameraErrorDomain]) {
            if(error.code == LLSimpleCameraErrorCodeCameraPermission ||
               error.code == LLSimpleCameraErrorCodeMicrophonePermission) {
                
                if(weakSelf.errorLabel) {
                    [weakSelf.errorLabel removeFromSuperview];
                }
                
                UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
                label.text = @"We need permission for the camera.\nPlease go to your settings.";
                label.numberOfLines = 2;
                label.lineBreakMode = NSLineBreakByWordWrapping;
                label.backgroundColor = [UIColor clearColor];
                label.font = [UIFont fontWithName:@"AvenirNext-DemiBold" size:13.0f];
                label.textColor = [UIColor whiteColor];
                label.textAlignment = NSTextAlignmentCenter;
                [label sizeToFit];
                label.center = CGPointMake(screenRect.size.width / 2.0f, screenRect.size.height / 2.0f);
                weakSelf.errorLabel = label;
                [weakSelf.view addSubview:weakSelf.errorLabel];
            }
        }
    }];
}

- (void)setupUI{
    self.listButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.listButton setImage:[UIImage imageNamed:@"greet_dynamic"] forState:UIControlStateNormal];
    [self.listButton addTarget:self action:@selector(listButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    //    [self.listButton setBackgroundColor:[UIColor whiteColor]];
    //    self.listButton.imageEdgeInsets = UIEdgeInsetsMake(10.0f, 10.0f, 10.0f, 10.0f);
    [self.view addSubview:self.listButton];
    [self.listButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(13);
        make.left.equalTo(self.view.mas_left).offset(10);
        make.width.and.height.equalTo(@44);
    }];
    
    self.switchToCameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.switchToCameraButton setImage:[UIImage imageNamed:@"greet_camera"] forState:UIControlStateNormal];
    [self.switchToCameraButton addTarget:self action:@selector(switchToCameraButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    //    [self.switchToCameraButton setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.switchToCameraButton];
    [self.switchToCameraButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.listButton.mas_top);
        make.right.equalTo(self.view.mas_right).offset(-10);
        make.width.and.height.equalTo(@44);
    }];
    
    self.selectToolsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.selectToolsButton setImage:[UIImage imageNamed:@"greet_tool"] forState:UIControlStateNormal];
    [self.selectToolsButton addTarget:self action:@selector(selectToolsButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    //    [ghostButton setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.selectToolsButton];
    [self.selectToolsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-10);
        make.centerX.equalTo(self.view);
        make.width.and.height.equalTo(@44);
    }];
    
    self.ghostButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.ghostButton setImage:[UIImage imageNamed:@"greet_ghost"] forState:UIControlStateNormal];
    [self.ghostButton addTarget:self action:@selector(ghostButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    //    [ghostButton setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.ghostButton];
    [self.ghostButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.listButton);
        make.centerX.equalTo(self.view);
        make.width.and.height.equalTo(@48);
    }];
    
    [self performSelector:@selector(popTipViewShowAt:) withObject:self.ghostButton afterDelay:0.5];
    
//    self.clearGreetingButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.clearGreetingButton.frame = CGRectMake(0, 0, 70, 70);
//    [ self.clearGreetingButton addTarget:self action:@selector(greetingButtonPressed) forControlEvents:UIControlEventTouchUpInside];
//    self.clearGreetingButtonWindow = [[UIWindow alloc]initWithFrame:CGRectMake(self.view.centerX - 35, kScreenHeight- 40- 70- 2, 70, 70)];
//    self.clearGreetingButtonWindow.windowLevel = UIWindowLevelAlert+1;
//    self.clearGreetingButtonWindow.backgroundColor = [UIColor clearColor];
//    self.clearGreetingButtonWindow.layer.cornerRadius = 35;
//    self.clearGreetingButtonWindow.layer.masksToBounds = YES;
//    [self.clearGreetingButtonWindow addSubview:self.clearGreetingButton];
    //    [self.clearGreetingButtonWindow makeKeyAndVisible];//关键语句,显示window
    
    
    [self greetingButtonAddLongPressGesture];
}

- (void)greetingButtonAddLongPressGesture{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(greetingButtonLongPressed)];
    //minimum press duration to call long gesture
    longPress.minimumPressDuration = 0.5;
    LVThreeBtnTool *threeBtnTool = [LVThreeBtnTool shareThreeBtn];
    [threeBtnTool.centerBtn addGestureRecognizer:longPress];
}

- (BOOL)prefersStatusBarHidden{
    return YES;
}
- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
